package org.wattdepot.cli.command;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Calculates the total power generated by a specified source or set of subsources at a 
 * specified time, if the source uses a specific fuel type. If a virtual source is input, 
 * all subsources of that virtual source will be used.
 * 
 * @author Kendyll Doi
 * @author Kimberly Heu
 *
 */
public class WattTotalPower {
  
  /**
   * Calculates the total power generated by a specified source or set of subsources at a 
   * specified time, if the source uses a specific fuel type. If a virtual source is input, 
   * all subsources of that virtual source will be used.
   * 
   * @param sourceName The source to get information for.
   * @param time The time to get the total power generated.
   * @param fuelType The fueltype to search for.
   * @param client The client used for the source information.
   * @return A string containing the result of running the command.
   */
  public static String wattTotalPower (String sourceName, String time, String fuelType, 
      WattDepotClient client) {
    
    StringBuffer buf = new StringBuffer();
    String output = "";
    XMLGregorianCalendar timestamp; // Timestamp to check for data
    Source source; // The source we are getting data on
    Double totalPower = 0.0;
    
    try {
      source = client.getSource(sourceName); // Initialize the source
      
      // Try to create a timestamp. If it is invalid, the method will return null
      // so return the appropriate error message.
      timestamp = CommandLineInterface.createTstamp(time);
      if (timestamp == null) {
        return ("Incorrect timestamp input.");
      }
      
      // Get a list of all subsources using this fuel type
      List<String> fuelSubsources = 
        WattSubsourcesFuel.getSubsourcesFuel(source, fuelType, client);
      
      // Go through each of the subsources and sum up their power generated
      for (String subString : fuelSubsources) {
        totalPower += client.getPowerGenerated(subString, timestamp);
      }
      
      // Append the result to the buffer
      buf.append(Double.toString(totalPower));
      
    }
    catch (ResourceNotFoundException e) {
      // Occurs if source does not exist
      return "No data found for " + sourceName;
    }
    catch (MiscClientException e) {
      // Occurs if error happens while getting data
      return "Error while retrieving resource";
    }
    catch (NotAuthorizedException e) {
      // Occurs if user is not authorized to get information
      return "Not authorized to retrieve information.";
    }
    catch (BadXmlException e) {
      // Occurs if data is corrupted
      return "Error enountered getting information from server.";
    }
    catch (Exception e) {
      // Catch any other errors
      return "Encountered error: " + e;
    }
    
    output = buf.toString();
    return output;
  }
}
